package jy.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jy.dao.FoodDaoI;
import jy.dao.FtypeDaoI;
import jy.dao.StoreDaoI;
import jy.model.Food;
import jy.model.Ftype;
import jy.model.Store;
import jy.model.User;
import jy.pageModel.PFood;
import jy.pageModel.PStore;
import jy.pageModel.PUser;
import jy.service.StoreServiceI;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StoreServiceImpl implements StoreServiceI {
	@Autowired
	private StoreDaoI storeDao;
	@Autowired
	private FoodDaoI foodDao;
	@Autowired
	private FtypeDaoI ftypeDao;
	@Override
	public PUser shopreg(PUser user,User u) {
		Store s = new Store();

		s.setUser(u);
		s.setName(user.getName());
		s.setStoreName(user.getStoreName());
		s.setOtel(user.getOtel());
		s.setAddress(user.getAddress());
		s.setServicetime(user.getServicetime());
		s.setMincost(user.getMincost());
		s.setTel(user.getTel());
		s.setDeliveryscost(user.getDeliveryscost());
		s.setInfo(user.getInfo());
		s.setImage(user.getImage());
		storeDao.save(s);
		return null;
	}

	public List searchname(String keyword){
		keyword = "麦当劳";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("keyword", "%%"+keyword.trim()+"%%");
		List<Store> result = storeDao.find("from Store t where t.storeName like :keyword order by name",params);
		List<PStore> stores = new ArrayList<PStore> ();
		
		for(Store s : result ){
			PStore store = new PStore();
			BeanUtils.copyProperties(s, store,new String[]{"ftypes"});
			stores.add(store);
		}
		return stores;
	}
	
	
	public void addFood(PFood food){
		
	}

	//获得商铺的食物信息
	public List<PFood> menu(String sid) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("sid", sid);
		List<Food> result = foodDao.find("from Food t where t.store.id = :sid ",params);
		
		List<PFood> menu = new ArrayList<PFood> ();	
		for(Food f : result ){
			PFood food = new PFood();
			BeanUtils.copyProperties(f, food);
			menu.add(food);
		}
		return menu;
	}

	//获得商铺名称电话等信息
	public PStore getInfo(String sid) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("sid", Integer.parseInt(sid));
		Store result = storeDao.find("from Store t where t.id = :sid ",params).get(0);
		PStore store = new PStore();
		BeanUtils.copyProperties(result, store,new String[]{"ftypes"});
		
		return store;
	}

	@Override
	public List<Ftype> getFtypes(String sid) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("sid", Integer.parseInt(sid));
		List<Ftype> result = ftypeDao.find("from Ftype t where t.store.id = :sid ",params);	
		return result;
	}
}
